package com.bdult.vh.common;

import java.util.Enumeration;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Service
public class Interceptor extends HandlerInterceptorAdapter {

	static Logger logger = LoggerFactory.getLogger(Interceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		printRequestLog(request);
		
		try {
			if(notExcludeRequestPath(request)){		//아래와 주석을 바꾸면 세션체크
			//if(false){							//세션체크 않할때
				logger.info("noHasSession(request) = " + noHasSession(request));
				if (noHasSession(request)){
					logger.info("notExcludeRequestPath and no session");
					request.getSession().setAttribute("member_id", null);
					response.sendRedirect("/vhmall/member/login.vh");
					return false;
				}
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
		
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterCompletion(request, response, handler, ex);
	}

	private void printRequestLog(HttpServletRequest request) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("[REQ]");
			sb.append("_IP_[" + request.getRemoteAddr() + "]");
			sb.append("_REQURL_[" + request.getRequestURI() + "]");
			sb.append("_PARAM_");

			// parameter
			Enumeration eNames = request.getParameterNames();
			while (eNames.hasMoreElements()) {
				String name = (String) eNames.nextElement();
				String[] values = request.getParameterValues(name);
				String paramIngo = "[" + name + " : ";
				for (int x = 0; x < values.length; x++) {
					if (x == 0) {
						paramIngo += values[x];
					} else {
						paramIngo += ", " + values[x];
					}
				}

				if (!StringUtils.isEmpty((name))) {
					if (name.equals("passWd")) {
						paramIngo = "[password : xxxx ]";
					} else {
						paramIngo += "]";
						
					}
				}

				if (eNames.hasMoreElements()) {
					sb.append(paramIngo + ",");
				} else {
					sb.append(paramIngo);
				}
			}
			sb.append("]");
			logger.info(sb.toString());
		} catch (Exception e) {
			logger.error("{}", e);
		}
	}
	
	private boolean notExcludeRequestPath(HttpServletRequest request) {
		//세션 체크 예외 리스트
		return 
		! request.getServletPath().contains( "login" ) &&
		! request.getServletPath().contains( "join" ) &&
		! request.getServletPath().contains( "logout" ) &&
		! request.getServletPath().contains( "tal" ) &&
		! request.getServletPath().contains( "smscert" ) &&
		! request.getServletPath().contains( "idcheck" ) &&
		! request.getServletPath().contains( "event_list" ) &&
		! request.getServletPath().contains( "promotion" ) &&
		! request.getServletPath().contains( "best" ) &&
		! request.getServletPath().contains( "showtime" ) &&
		! request.getServletPath().contains( "idFind" ) &&
		! request.getServletPath().contains( "pwFind" ) &&
		! request.getServletPath().contains( "wishlist" ) &&
		! request.getServletPath().contains( "item_detail" ) &&
		! request.getServletPath().contains( "cart" ) &&
		! request.getServletPath().contains( "order" ) &&
		! request.getServletPath().contains( "orderNoAjax" ) &&
		! request.getServletPath().contains( "startPG" ) &&
		! request.getServletPath().contains( "returnurl" ) &&
		! request.getServletPath().contains( "note_url" ) &&
		! request.getServletPath().contains( "payres" ) &&
		! request.getServletPath().contains( "cancel_url" ) &&
		! request.getServletPath().contains( "category" ) &&
		! request.getServletPath().contains( "notice" ) &&
		! request.getServletPath().contains( "jsonView" ) &&
		! request.getServletPath().contains( "zipcode" ) &&
		! request.getServletPath().contains( "main" );
		
//		return false;
	}

	private boolean noHasSession(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		return session == null || session.getAttribute("member_id") == null;
	}

}



	